/**
 * 
 */
package org.cuatrovientos.erp.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.DelegationBOInterface;
import org.cuatrovientos.erp.model.Delegation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Delegation management
 * @author Natalia Bernarte
 */
@Controller
public class DelegationController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(DelegationController.class);

	@Autowired
    private DelegationBOInterface delegationBO;
    
        /**
         * default constructor
         */
        public DelegationController () {
               logger.info("Delegation BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /delegation or /delegation/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/delegation","/delegation/list"})
         */
        @RequestMapping({"/shipment","*","/shipment/list"})
        public ModelAndView delegationDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Delegation> shipmentList = delegationBO.selectAll();
                model.put("delegations", shipmentList );
                return new ModelAndView("DelegationList", model);     
        }

        /**
         * handles /delegation/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/delegation/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/delegation/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Delegation> delegationList = delegationBO.search(description);
            model.put("delegations", delegationList );
            
            return new ModelAndView("delegationList", model);     
        }

        /**
         * handles delete delegation, and redirect to delegation default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/delegation","/delegation/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/delegation/delete"})
        public String deleteDelegation(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            delegationBO.deleteById(id);
            return "redirect:/delegation";

        }
        
        /**
         * handles new delegation request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "delegation"
         * @return the name of the view to show
         * @RequestMapping({"/delegation/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/delegation/new"})
        public ModelAndView newShipment() {
        		logger.info("new delegation ");
                Delegation delegation = new Delegation();
               
                return new ModelAndView("NewDelegation", "delegation", delegation);
        }
        
        /**
         * handles new delegation request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/delegation/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/delegation/save"})
        public String newDelegation(@Valid Delegation delegation, BindingResult bindingResult) {
        		logger.info("new delegation " + delegation.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("delegation", delegation);
        			return "NewDelegation";
        		}
        		
        		// If data is ok, insert and go on
                delegationBO.insert(delegation);
                return "redirect:/delegation";
        }
        
        /**
         * handles delete delegation, and redirect to delegation default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/delegation","/delegation/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/delegation/detail"})
        public ModelAndView delegationDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Delegation delegation = delegationBO.selectById(id);
            model.put("delegation", delegation);
            
            return new ModelAndView("DelegationDetail",model);

        }
        
        /**
         * handles delegation update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/delegation/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/delegation/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Delegation delegation = delegationBO.selectById(id);
                model.put("delegation", delegation);
               return new ModelAndView("UpdateDelegation",model);

        }
        
        /**
         * handles save update delegation request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/delegation/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/delegation/saveupdate"})
        public String saveUpdate(@Valid Delegation delegation, BindingResult bindingResult) {
        		logger.info("update delegation " + delegation.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("delegation", delegation);
        			return "UpdateDelegation";
        		}
        		
                delegationBO.update(delegation);
                return "redirect:/delegation";
        }
}
